package cn.picclife.mwx.common.core.util.xml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.google.common.base.Throwables;

/**
 * ClassName: JAXBUtil <br/>
 * date: 2016年9月8日 下午2:07:01 <br/>
 *
 * XML和JAVABEAN处理类
 */
public class JAXBUtil {

    /** 缺省字符集 **/
    private static final String DEFAULT_ENCODING = "UTF-8";

    public static <T> Object unmarshallFromFile(String xmlPath, Class<T> clazz) {
        Object xmlObject = null;
        try {
            JAXBContext jc = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File file = new File(xmlPath);
            xmlObject = unmarshaller.unmarshal(file);
        } catch (Exception e) {
            Throwables.propagate(e);
        }
        return xmlObject;
    }

    public static <T> Object unmarshallFromXml(String xmlStr, Class<T> clazz) {
        return unmarshallFromXml(xmlStr, clazz, DEFAULT_ENCODING);
    }

    public static <T> Object unmarshallFromXml(String xmlStr, Class<T> clazz, String encoding) {
        Object xmlObject = null;
        try {
            JAXBContext jc = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            xmlObject = unmarshaller.unmarshal(new ByteArrayInputStream(xmlStr.getBytes(encoding)));
        } catch (Exception e) {
            Throwables.propagate(e);
        }
        return xmlObject;
    }

    public static <T> String marshall(Object obj, Class<T> clazz) {
        return marshall(obj, clazz, DEFAULT_ENCODING);
    }

    public static <T> String marshall(Object obj, Class<T> clazz, String encoding) {
        String xml = null;
        try {
            StringWriter writer = new StringWriter();
            JAXBContext context = JAXBContext.newInstance(clazz);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.setProperty(Marshaller.JAXB_ENCODING, encoding);
            m.marshal(obj, writer);
            xml = writer.toString();
        } catch (Exception e) {
            Throwables.propagate(e);
        }
        return xml;
    }
       /**
     * beanToXml
     * @param obj
     * @return
     */
    /*public static String convertToXml(Object obj,String encode)  {
        // 创建输出流  
        StringWriter writer = null;  
        try {  
            // 利用jdk中自带的转换类实现  
            JAXBContext context = JAXBContext.newInstance(obj.getClass());  
            Marshaller marshaller = context.createMarshaller();  
            // 格式化xml输出的格式  
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encode);
              writer = new StringWriter();
            // 将对象转换成输出流形式的xml  
            marshaller.marshal(obj, writer);  
        } catch (JAXBException e) {  
            e.printStackTrace();  
        }  
        return new String(writer.toString()).replace(" standalone=\"yes\"", "");  
    }*/
    /** 
     * xml转换成JavaBean 
     * @param xml 
     * @param c 
     * @return 
     */  
    /*@SuppressWarnings("unchecked")
    public static <T> T converyToJavaBean(String xml, Class<T> c) {
        T t = null;
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            t = (T) unmarshaller.unmarshal(new StringReader(xml));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return t;
    }  */
}
